import 'package:flutter/material.dart';
import 'package:collection/collection.dart';

class Pager extends StatefulWidget {

  late TextStyle activeTextStyle;  // 当前页码的文本样式
  late Widget preWidget;  // 上一页的widget
  late Widget nextWidget; // 下一页的widget
  int total;  // 总页码数量
  int totalNum;  // 总页码数量
  late double containerWidth;  // 容器的宽度
  late MainAxisAlignment mainAxisAlignment;  // 页码横主轴对齐方式
  late TextStyle normalTextStyle;  // 其他页码的文本样式
  late double pagerItemWidth;  // 页码item的宽度
  late double pagerItemHeight; // 页码item的高度
  late BoxDecoration pagerItemDecoration;  // 页码item的样式
  ValueChanged pageChange;   // 页码改变的回调
  late EdgeInsetsGeometry pagerItemMargin;  // 页码item的边距
  late BoxDecoration pagerItemDecorationNormal;  // 页码item的样式
  Pager({
    Key? key,
    TextStyle? activeTextStyle,
    Widget? preWidget,
    Widget? nextWidget,
    required this.total,
    required this.totalNum,
    double? containerWidth,
    MainAxisAlignment? mainAxisAlignment,
    TextStyle? normalTextStyle,
    double? pagerItemWidth,
    double? pagerItemHeight,
    BoxDecoration? pagerItemDecoration,
    required this.pageChange,
    EdgeInsetsGeometry? pagerItemMargin
  }) :  activeTextStyle = activeTextStyle ?? TextStyle(color: Color(0xFF1664FF),
      fontSize: 14),
        preWidget = preWidget ?? Icon(Icons.arrow_left, color: Color(0xFF4E5969), size: 16),
        nextWidget = nextWidget ?? Icon(Icons.arrow_right, color: Color(0xFF4E5969), size: 16),
        containerWidth = containerWidth ?? double.infinity,
        mainAxisAlignment = mainAxisAlignment ?? MainAxisAlignment.spaceAround,
        normalTextStyle = normalTextStyle ?? TextStyle(color: Color(0xFF1D2129), fontSize: 14),
        pagerItemWidth = pagerItemWidth ?? 36,
        pagerItemHeight = pagerItemHeight ?? 36,
        pagerItemDecoration = pagerItemDecoration ?? BoxDecoration(color: Color(0xFFE8F4FF), borderRadius: BorderRadius.circular(0)),
        pagerItemDecorationNormal = pagerItemDecoration ?? BoxDecoration(color: Colors.transparent, borderRadius: BorderRadius.circular(0)),
        pagerItemMargin = pagerItemMargin ?? const EdgeInsets.all(0),
        super(key: key);

  @override
  State<Pager> createState() => _PagerState();
}

class _PagerState extends State<Pager> {
  int _pageIndex = 1;
  List _list = [];


  void manageList() {
    _list = [];
    // if(widget.total <= 1) {
    //   return;
    // }
    if(widget.total <= 7) {
      for(var i = 0; i < widget.total; i++) {
        _list.add(i+1);
      }
    } else if(_pageIndex <= 4) {
      _list = [1,2,3,4,5, "...", widget.total];
    } else if(_pageIndex > widget.total - 3) {
      _list = [1,2, "...", widget.total - 3, widget.total - 2, widget.total - 1,  widget.total];
    } else {
      _list = [1, "...", _pageIndex - 1, _pageIndex, _pageIndex + 1, "...", widget.total];
    }
    _list.insert(0, widget.preWidget);
    _list.add(widget.nextWidget);
  }

  @override
  Widget build(BuildContext context) {
    return buildContent();
  }


  Widget buildContent() {
    manageList();
    return Container(

      width: widget.containerWidth,

      child:Row(
        mainAxisSize: MainAxisSize.min,
        mainAxisAlignment: widget.mainAxisAlignment,
        children: [
          Text('共'+widget.totalNum.toString()+'条',style:TextStyle(color:Color(0xFF1D2129),fontSize: 14 )),
           Row(
            children: _list.mapIndexed((index, ele) {
              if(index == 0) {
                return GestureDetector(
                  onTap: () {
                    if(_pageIndex > 1) {
                      setState(() {
                        _pageIndex--;
                        widget.pageChange(_pageIndex);
                      });
                    }
                  },
                  child: buildPagerItem(
                      child: ele,
                      isSelected:ele == _pageIndex
                  ),
                );
              }
              if(index == _list.length - 1) {
                return GestureDetector(
                  onTap: () {
                    if(_pageIndex < widget.total) {
                      setState(() {
                        _pageIndex++;
                        widget.pageChange(_pageIndex);
                      });
                    }
                  },
                  child: buildPagerItem(
                      child: ele,
                      isSelected:ele == _pageIndex
                  ),
                );
              }
              return GestureDetector(
                onTap: () {
                  if(ele != "...") {
                    setState(() {
                      if(_pageIndex != ele) {
                        _pageIndex = ele;
                        widget.pageChange(_pageIndex);
                      }
                    });
                  }
                },
                child: buildPagerItem(
                    child: Text(
                      "$ele",
                      style: ele == _pageIndex ? widget.activeTextStyle : widget.normalTextStyle,
                    ),
                    isSelected:ele == _pageIndex
                ),
              );
            }).toList(),
          )
        ],
      )
     ,
    );
  }


  Widget buildPagerItem({
    required Widget child,required bool isSelected
  }) {
    return Container(
      width: widget.pagerItemWidth,
      height: widget.pagerItemHeight,
      alignment: Alignment.center,
      decoration: isSelected?widget.pagerItemDecoration:widget.pagerItemDecorationNormal,
      margin: widget.pagerItemMargin,
      child: child,
    );
  }
}

